<?php
require_once("log.change.class.php");
class ugovori_save extends ugovori{
	
	var $_data;
	var $queries = array(
			"delete" => array(),
			"insert" => array(),
			"update" => array()
		);
	var $error = array(
		"code" => 0,
		"message" => ""
	);
	
	/**
	 * LOG CHANGE object
	 *
	 * @var Object
	 */
	var $log_change;
	
	function ugovori_save($data){
		$this->db = new db_functions();
		$this->log_change = new log_change();
		
		error_reporting(0);
		
		$this->log_change->state = 2;
		
		$this->parseInputData($data);
		
		/*if (intval($this->_data->id)>0) {
			file_put_contents("2.txt",var_export($this->_data,true));
			//file_put_contents("2.txt",var_export($_POST,true));
		}else{
			file_put_contents("1.txt",var_export($this->_data,true));
			//file_put_contents("1.txt",var_export($_POST,true));
		}*/
		
		$test = $this->testData();
		if ($test !== true) {
			die("1{!}$test");
		}
		
		if (!$this->_data->id) {
			if(!$this->novi()){
				if ($this->error["code"] = 1062) {
					die("1{!}Ugovor broj ".$this->_data->broj." već postoji.");
				}else{
					die("1{!}".$this->error["message"]);
				}
			}
		}
		$this->generateQueries();
		//echo "1{!}";print_rarray($this->queries);die();
		if (!$this->write()) {
			$out = $this->error["code"]."{!}".$this->error["message"];
			die($out);
		}else{
			echo "0{!}Ugovor sačuvan.{!}".$this->_data->id;
		}
	}
	
	function parseInputData($data){
		$_data = array();
		
		foreach ($_POST as $k=>$v){
			$v = utf8RawUrlDecode($v);
			$v = utf8RawChrDecode($v);
			$_data[$k] = trim($v);
		}
		
		//$this->debug(var_export($_POST,true));
		
		foreach ($_data as $k=>$v){
			$tmp = preg_split('/(_\d+)/',$k,-1,PREG_SPLIT_DELIM_CAPTURE);
			array_removeEmptyItems($tmp);
			$tmp = array_values($tmp);
			
			$varname = array_splice($tmp,0,1);
			$varname = $varname[0];
			
			if (count($tmp)>0) {
				
				foreach ($tmp as $m=>$n){
					$tmp[$m] = intval(preg_replace('/[^\d]/','',$n));
				}
				//echo "$k: ".count($tmp).", $varname<br />";
				switch (count($tmp)){
					case 1:
						$this->_data[$varname][$tmp[0]] = $v;
						break;
					case 2:
						$this->_data[$varname][$tmp[0]][$tmp[1]] = $v;
						break;
					case 3:
						$this->_data[$varname][$tmp[0]][$tmp[1]][$tmp[2]] = $v;
						break;
				}
			}else{
				$this->_data[$varname] = $v;
			}
		}
		$_data = array();
		foreach ($this->_data as $k=>$v){
			if (is_array($v)) {
				if($k=="book_links"){
					foreach ($v as $m=>$n){
						$_data[$k][$m] = $n;
					}
				}else{
					$_data[$k] = array_values($v);
				}
			}else{
				$_data[$k] = $v;
			}
		}
		//$this->debug(var_export($this->_data,true));
		$this->debug(var_export($_data,true));
		// FILL THE DATA OBJECT
		$this->_data = new voomy();
		
		$this->_data->id = $_data["ugovor_id"]>0?$_data["ugovor_id"]:null;
		$this->_data->broj = trim($_data["ugovor_broj"]);
		$this->_data->komitent = array(
			"id"	=> $_data["komitent_id"],
			"name"	=> $_data["komitent_name"],
			"ziro"	=> $_data["komitent_ziro"],
			"banka"	=> $_data["komitent_banka"]
		);
		$this->_data->datum = array(
			"date"	=> $_data["ugovor_datum"],
			"time"	=> date2int($_data["ugovor_datum"])
		);
		$this->_data->cenovnik = array(
			"id"	=> $_data["ugovor_cenovnik_id"],
			"date"	=> $_data["ugovor_cenovnik_date"],
			"time"	=> date2int($_data["ugovor_cenovnik_date"])
		);
		$this->_data->tip = $_data["ugovor_tip"];
		$this->_data->problem = $_data["ugovor_problem"];
		
		$this->_data->lokacija = array(
			"lid"	=> $_data["ugovor_lid"],
			"type"	=> $_data["ugovor_lok_type"]
		);
		$this->_data->radnik = $_data["ugovor_radnik"];
		$this->_data->porez = array(
			"id"	=> $_data["ugovor_porez"],
			"perc"	=> $_data["ugovor_porez_perc"]
		);
		$this->_data->state = $_data["ugovor_izrada_status"];
		$this->_data->obnova_popust = $_data["ugovor_obnova_popust"];
		$this->_data->ne_obnova = $_data["ugovor_ne_obnova"];
		$this->_data->napomena = $_data["ugovor_napomena"];
		
		$this->_data->cena = array(
			"bruto"		=> $_data["ugovor_iznos_bruto"],
			"korekcija"	=> $_data["ugovor_iznos_korekcija"],
			"porez"		=> $_data["ugovor_iznos_porez"],
			"neto"		=> $_data["ugovor_iznos_neto"],
		);
		
		// DOPISI
		$this->_data->dopisi = array();
		$len = $_data["dopisi_length"];
		for ($i=0; $i<$len; $i++){
			$this->_data->dopisi[$i] = array(
				"tip"			=> $_data["dopis_tip"][$i],
				"date"			=> $_data["dopis_date"][$i],
				"radnik"		=> $_data["dopis_radnik"][$i],
				"radnik_date"	=> $_data["dopis_radnik_date"][$i],
				"ishod"			=> $_data["dopis_ishod"][$i],
				"ishod_date"	=> $_data["dopis_ishod_date"][$i],
				"napomena"		=> $_data["dopis_napomena"][$i]
			);
		}
		
		// DEKLARACIJE
		$this->_data->deklaracije = array();
		$len = $_data["deklaracije_length"];
		for ($i=0; $i<$len; $i++){
			$this->_data->deklaracije[$i] = array(
				"creator" => array(
					"name"			=> $_data["dekl_creator_name"][$i],
					"address"		=> $_data["dekl_creator_adresa"][$i],
					"del_id"		=> $_data["dekl_creator_del_id"][$i],
					"del_name"		=> $_data["dekl_creator_del_name"][$i],
					"matbr"			=> $_data["dekl_creator_matbr"][$i],
					"odg_ime"		=> $_data["dekl_creator_odg_ime"][$i],
					"odg_address"	=> $_data["dekl_creator_odg_adresa"][$i],
					"odg_jmbg"		=> $_data["dekl_creator_odg_jmbg"][$i],
					"odg_lk"		=> $_data["dekl_creator_odg_lk"][$i]
				),
				"owner" => array(
					"name"			=> $_data["dekl_name"][$i],
					"address"		=> $_data["dekl_adresa"][$i],
					"del_id"		=> $_data["dekl_del_id"][$i],
					"del_name"		=> $_data["dekl_del_name"][$i],
					"matbr"			=> $_data["dekl_matbr"][$i],
					"odg_ime"		=> $_data["dekl_odg_ime"][$i],
					"odg_address"	=> $_data["dekl_odg_adresa"][$i],
					"odg_jmbg"		=> $_data["dekl_odg_jmbg"][$i],
					"odg_lk"		=> $_data["dekl_odg_lk"][$i]
				),
				"message" => array(
					"phone"			=> $_data["dekl_tel"][$i],
					"date"			=> $_data["dekl_datum"][$i],
					"time"			=> date2int($_data["dekl_datum"][$i]),
					"location"		=> $_data["dekl_mesto"][$i],
					"signature"		=> $_data["dekl_potpis"][$i],
					"text"			=> $_data["dekl_text"][$i],
					"type"			=> $_data["dekl_msg_tip"][$i]
				)
			);
		}		
		// STAVKE
		for ($i=0; $i<$_data["items_length"]; $i++){
			
			// paket
			$this->_data->stavke[$i]["paket"] = intval($_data["stavka_paket"][$i]);
			
			// artikl (ako je paket onda sifra paketa, a ako je artikl onda sifra cene)
			$this->_data->stavke[$i]["artikl"] = intval($_data["stavka_artikl"][$i]);
			
			// kolicina
			$this->_data->stavke[$i]["kolicina"] = intval($_data["stavka_kol"][$i]);
			
			// neto
			$this->_data->stavke[$i]["cena"] = floatval(strip_number_format($_data["stavka_cena"][$i]));
			
			// popust
			$this->_data->stavke[$i]["popust"] = floatval(strip_number_format($_data["stavka_popust"][$i]));
			
			// porez
			$this->_data->stavke[$i]["porez"] = floatval(strip_number_format($_data["stavka_porez"][$i]));
						
			// bruto
			$this->_data->stavke[$i]["bruto"] = floatval(strip_number_format($_data["stavka_ukupno"][$i]));
			
			// komitent
			$this->_data->stavke[$i]["komitent"]["id"] = intval($_data["stavka_komitent_id"][$i]);
			$this->_data->stavke[$i]["komitent"]["name"] = $_data["stavka_komitent_name"][$i];
			
			// kampanja
			$this->_data->stavke[$i]["kampanja"] = $_data["stavka_kampanja"][$i];
			
			// delatnost
			$this->_data->stavke[$i]["delatnost"] = $_data["stavka_delatnost"][$i];
			// tok realizacije data
				
				// ARTIKL TOK DATA
				if ($this->_data->stavke[$i]["paket"]==0) {
					
					// materijal
					if (count($_data["mat_data"][$i])>0) {
						foreach ($_data["mat_data"][$i] as $k=>$matdata){
							$_artid = $_data["tok_art_id"][$i];
							$this->_data->stavke[$i]["tok_data"]["materijal"][$k] = $this->VSON($matdata);
						}
					}
					
					// materijal se ocekuje
					$this->_data->stavke[$i]["tok_data"]["materijal_ocekuje"] = array(
						"date" => $_data["mat_ocekuje"][$i],
						"time" => date2int($_data["mat_ocekuje"][$i])
					);
					
					// komitent poslato
					$this->_data->stavke[$i]["tok_data"]["komitent_poslato"] = array(
						"date" => $_data["komitent_poslato"][$i],
						"time" => date2int($_data["komitent_poslato"][$i])
					);
					
					// komitent obavesten
					$this->_data->stavke[$i]["tok_data"]["komitent_obavesten"] = array(
						"date" => $_data["komitent_obavesten"][$i],
						"time" => date2int($_data["komitent_obavesten"][$i])
					);
					
					// komitent potvrdjeno
					$this->_data->stavke[$i]["tok_data"]["komitent_potvrdjeno"] = array(
						"date" => $_data["komitent_potvrdjeno"][$i],
						"time" => date2int($_data["komitent_potvrdjeno"][$i])
					);
					
					// komitent obnova
					$this->_data->stavke[$i]["tok_data"]["obnova"] = intval($_data["komitent_obnova"][$i]);
					
					// specijalni zahtevi
					$this->_data->stavke[$i]["tok_data"]["spec_zahtevi"] = $_data["spec_zah"][$i];
					
					// napomena
					$this->_data->stavke[$i]["tok_data"]["napomena"] = $_data["napomena"][$i];
					
					// akcije dizajnera
					if (count($_data["act_data"][$i])>0) {
						foreach ($_data["act_data"][$i] as $k=>$actdata){
							$this->_data->stavke[$i]["tok_data"]["akcije_dizajnera"][$k] = $this->VSON($actdata);
						}
					}
					
					// INTERNET DATA
						
						// periodi trajanja
						$tmp = $_data["internet_data_periodi"][$i];
						$rows = explode("{!!}",$tmp);
						foreach ($rows as $k=>$cell){
							if(preg_match('/\{!\}/',$cell)){
								$tmp1 = explode("{!}",$cell);
								$this->_data->stavke[$i]["tok_data"]["internet_data"]["periodi"][$k]["od"] = array(
									"date" => $tmp1[0],
									"time" => date2int($tmp1[0])
								);
								$this->_data->stavke[$i]["tok_data"]["internet_data"]["periodi"][$k]["do"] = array(
									"date" => $tmp1[1],
									"time" => date2int($tmp1[1])
								);
							}
						}
						
						// kljucne reci
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["kwords"] = $_data["kwords"][$i];
						
						// tip profila
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["tip"] = $_data["profile_tip"][$i];
						
						// profil start
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["start"] = array(
							"date" => $_data["profile_start"][$i],
							"time" => date2int($_data["profile_start"][$i])
						);
						
						// profil end
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["end"] = array(
							"date" => $_data["profile_end"][$i],
							"time" => date2int($_data["profile_end"][$i])
						);
						
						// logo
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["logo"] = $_data["profile_logo"][$i];
						
						// profil srp
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["srp"] = $_data["profile_text_srp"][$i];
						
						// profil eng
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["eng"] = $_data["profile_text_eng"][$i];
						
						// tip banera
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["baner"]["tip"] = $_data["baner_tip"][$i];
						
						// baner start
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["baner"]["start"] = array(
							"date" => $_data["baner_start"][$i],
							"time" => date2int($_data["baner_start"][$i])
						);
							
						// baner end
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["baner"]["end"] = array(
							"date" => $_data["baner_end"][$i],
							"time" => date2int($_data["baner_end"][$i])
						);
						
						// baner
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["baner"]["name"] = $_data["baner_name"][$i];
						
						// test adresa
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["test_adresa"] = $_data["test_adresa"][$i];
						
						// konacna adresa
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["final_adresa"] = $_data["final_adresa"][$i];
						
						// hosting yp start
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["yp"]["start"] = array(
							"date" => $_data["host_yp_start"][$i],
							"time" => date2int($_data["host_yp_start"][$i])
						);
						
						// hosting yp end
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["yp"]["end"] = array(
							"date" => $_data["host_yp_end"][$i],
							"time" => date2int($_data["host_yp_end"][$i])
						);
						
						// hosting provajder
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["provajder"]["id"] = $_data["host_provider"][$i];
						
						// hosting start
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["provajder"]["start"] = array(
							"date" => $_data["host_start"][$i],
							"time" => date2int($_data["host_start"][$i])
						);
						
						// hosting end
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["provajder"]["end"] = array(
							"date" => $_data["host_end"][$i],
							"time" => date2int($_data["host_end"][$i])
						);
						
						// domen
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["domen"]["ime"] = $_data["domen_name"][$i];
						
						// domen start
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["domen"]["start"] = array(
							"date" => $_data["domen_start"][$i],
							"time" => date2int($_data["domen_start"][$i])
						);
						
						// domen end
						$this->_data->stavke[$i]["tok_data"]["internet_data"]["domen"]["end"] = array(
							"date" => $_data["domen_end"][$i],
							"time" => date2int($_data["domen_end"][$i])
						);
						
					// BOOK DATA
						
						// multi
						$this->_data->stavke[$i]["tok_data"]["book_data"]["multi"]["on"] = $_data["multi"][$i];
						
						// multi num pages
						$this->_data->stavke[$i]["tok_data"]["book_data"]["multi"]["num_pages"] = $_data["multi_num_pages"][$i];
						
						// multi delatnost
						$this->_data->stavke[$i]["tok_data"]["book_data"]["multi"]["delatnost"] = $_data["multi_start_del"][$i];
						
						// brands
						$this->_data->stavke[$i]["tok_data"]["book_data"]["brands"] = $_data["brands"][$i];
						
						// book links
						$this->_data->stavke[$i]["tok_data"]["book_data"]["links"] = $_data["book_links"][$i+1];
					
				}
				// PAKET TOK DATA
				else{
					// broj artikala u okviru paketa
					$broj_artikala = count($_data["mat_ocekuje"][$i]);
					
					for ($j=0; $j<$broj_artikala; $j++){
						
						// sifra artikla
						$_artid = $_data["tok_art_id"][$i][$j];
						$this->_data->stavke[$i]["tok_data"]["sifre_artikla"][$j] = $_artid;
						//[$j][$k]["artikl"] = $_artid;
						
						// materijal
						if(count($_data["mat_data"][$i][$j])){
							foreach ($_data["mat_data"][$i][$j] as $k=>$l){
								//$_pakid = $this->_data->stavke[$j]["artikl"];
								$this->_data->stavke[$i]["tok_data"]["materijal"][$j][$k] = $this->VSON($l);
							}
						}
						
						// materijal se ocekuje
						$this->_data->stavke[$i]["tok_data"]["materijal_ocekuje"][$j] = array(
							"date" => $_data["mat_ocekuje"][$i][$j],
							"time" => date2int($_data["mat_ocekuje"][$i][$j])
						);
						
						// komitent poslato
						$this->_data->stavke[$i]["tok_data"]["komitent_poslato"][$j] = array(
							"date" => $_data["komitent_poslato"][$i][$j],
							"time" => date2int($_data["komitent_poslato"][$i][$j])
						);
						
						// komitent obavesten
						$this->_data->stavke[$i]["tok_data"]["komitent_obavesten"][$j] = array(
							"date" => $_data["komitent_obavesten"][$i][$j],
							"time" => date2int($_data["komitent_obavesten"][$i][$j])
						);
						
						// komitent potvrdjeno
						$this->_data->stavke[$i]["tok_data"]["komitent_potvrdjeno"][$j] = array(
							"date" => $_data["komitent_potvrdjeno"][$i][$j],
							"time" => date2int($_data["komitent_potvrdjeno"][$i][$j])
						);
						
						// komitent obnova
						$this->_data->stavke[$i]["tok_data"]["obnova"][$j] = intval($_data["komitent_obnova"][$i][$j]);
						
						// specijalni zahtevi
						$this->_data->stavke[$i]["tok_data"]["spec_zahtevi"][$j] = $_data["spec_zah"][$i][$j];
						
						// napomena
						$this->_data->stavke[$i]["tok_data"]["napomena"][$j] = $_data["napomena"][$i][$j];
						
						
						// akcije dizajnera
						if (count($_data["act_data"][$i][$j])>0) {
							foreach ($_data["act_data"][$i][$j] as $k=>$actdata){
								$this->_data->stavke[$i]["tok_data"]["akcije_dizajnera"][$j][$k] = $this->VSON($actdata);
							}
						}
						
						
						// INTERNET DATA
						
							// periodi trajanja
							$tmp = $_data["internet_data_periodi"][$i][$j];
							$rows = explode("{!!}",$tmp);
							foreach ($rows as $k=>$cell){
								if(preg_match('/\{!\}/',$cell)){
									$tmp1 = explode("{!}",$cell);
									$this->_data->stavke[$i]["tok_data"]["internet_data"]["periodi"][$j][$k]["od"] = array(
										"date" => $tmp1[0],
										"time" => date2int($tmp1[0])
									);
									$this->_data->stavke[$i]["tok_data"]["internet_data"]["periodi"][$j][$k]["do"] = array(
										"date" => $tmp1[1],
										"time" => date2int($tmp1[1])
									);
								}
							}
							
							// kljucne reci
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["kwords"][$j] = $_data["kwords"][$i][$j];
							
							// tip profila
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["tip"][$j] = $_data["profile_tip"][$i][$j];
							
							// profil start
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["start"][$j] = array(
								"date" => $_data["profile_start"][$i][$j],
								"time" => date2int($_data["profile_start"][$i][$j])
							);
							
							// profil end
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["end"][$j] = array(
								"date" => $_data["profile_end"][$i][$j],
								"time" => date2int($_data["profile_end"][$i][$j])
							);
							
							// logo
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["logo"][$j] = $_data["profile_logo"][$i][$j];
							
							// profil srpski
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["srp"][$j] = $_data["profile_text_srp"][$i][$j];
							
							// profil engleski
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["profil"]["eng"][$j] = $_data["profile_text_eng"][$i][$j];
							
							// tip banera
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["baner"]["tip"][$j] = $_data["baner_tip"][$i][$j];
							
							// baner start
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["baner"]["start"][$j] = array(
								"date" => $_data["baner_start"][$i][$j],
								"time" => date2int($_data["baner_start"][$i][$j])
							);
							
							// baner end
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["baner"]["end"][$j] = array(
								"date" => $_data["baner_end"][$i][$j],
								"time" => date2int($_data["baner_end"][$i][$j])
							);
							
							// baner
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["baner"]["name"][$j] = $_data["baner_name"][$i][$j];
							
							// test adresa
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["test_adresa"][$j] = $_data["test_adresa"][$i][$j];
							
							// konacna adresa
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["final_adresa"][$j] = $_data["final_adresa"][$i][$j];
							
							// hosting yp start
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["yp"]["start"][$j] = array(
								"date" => $_data["host_yp_start"][$i][$j],
								"time" => date2int($_data["host_yp_start"][$i][$j])
							);
							
							// hosting yp end
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["yp"]["end"][$j] = array(
								"date" => $_data["host_yp_end"][$i][$j],
								"time" => date2int($_data["host_yp_end"][$i][$j])
							);
							
							// hosting provajder
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["provajder"]["id"][$j] = $_data["host_provider"][$i][$j];
							
							// hosting start
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["provajder"]["start"][$j] = array(
								"date" => $_data["host_start"][$i][$j],
								"time" => date2int($_data["host_start"][$i][$j])
							);
							
							// hosting end
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["hosting"]["provajder"]["end"][$j] = array(
								"date" => $_data["host_end"][$i][$j],
								"time" => date2int($_data["host_end"][$i][$j])
							);
							
							// domen
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["domen"]["ime"][$j] = $_data["domen_name"][$i][$j];
							
							// domen start
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["domen"]["start"][$j] = array(
								"date" => $_data["domen_start"][$i][$j],
								"time" => date2int($_data["domen_start"][$i][$j])
							);
							
							// domen end
							$this->_data->stavke[$i]["tok_data"]["internet_data"]["domen"]["end"][$j] = array(
								"date" => $_data["domen_end"][$i][$j],
								"time" => date2int($_data["domen_end"][$i][$j])
							);
						
						// BOOK DATA
							// multi
							$this->_data->stavke[$i]["tok_data"]["book_data"]["multi"]["on"][$j] = $_data["multi"][$i][$j];
							
							// multi num pages
							$this->_data->stavke[$i]["tok_data"]["book_data"]["multi"]["num_pages"][$j] = $_data["multi_num_pages"][$i][$j];
							
							// multi delatnost
							$this->_data->stavke[$i]["tok_data"]["book_data"]["multi"]["delatnost"][$j] = $_data["multi_start_del"][$i][$j];
							
							// brands
							$this->_data->stavke[$i]["tok_data"]["book_data"]["brands"][$j] = $_data["brands"][$i][$j];
							
							// book links
							$this->_data->stavke[$i]["tok_data"]["book_data"]["links"][$j] = $_data["book_links"][$i+1][$j];
					}
				}
		}
		
		// PLAN OTPLATE
		$this->_data->plan["broj_rata"] = intval($_data["plan_br_rata"]);
		$this->_data->plan["nacin_placanja"] = $_data["plan_nacin_placanja"];
		$this->_data->plan["avans"] = array(
			"bruto" => floatval(strip_number_format($_data["plan_avans_bruto"])),
			"neto"  => floatval(strip_number_format($_data["plan_avans_neto"])),
			"porez" => floatval(strip_number_format($_data["plan_avans_porez"]))
		);
		$this->_data->plan["avans_date"] = array(
			"date" => $_data["avans_date"],
			"time" => date2int($_data["avans_date"])
		);
		
		if ($this->_data->plan["broj_rata"]>0) {
			for ($i=0; $i<$this->_data->plan["broj_rata"]; $i++){
				$this->_data->plan["rate"][$i]["datum"] = array(
					"date" => $_data["plan_datum"][$i],
					"time" => date2int($_data["plan_datum"][$i])
				);
				$this->_data->plan["rate"][$i]["neto"]		= floatval(strip_number_format($_data["plan_neto"][$i]));
				$this->_data->plan["rate"][$i]["bruto"]		= floatval(strip_number_format($_data["plan_bruto"][$i]));
				$this->_data->plan["rate"][$i]["porez"]		= floatval(strip_number_format($_data["plan_porez"][$i]));
				$this->_data->plan["rate"][$i]["kamata"]	= floatval(strip_number_format($_data["plan_kamata"][$i]));
				$this->_data->plan["rate"][$i]["troskovi"]	= floatval(strip_number_format($_data["plan_troskovi"][$i]));
				$this->_data->plan["rate"][$i]["potvrda"]	= floatval(strip_number_format($_data["plan_potvrda"][$i]));
			}
		}
		
		// OTPLATA
		if (count($_data["otplata_id"])>0) {
			foreach ($_data["otplata_id"] as $k=>$otpid){
				$this->_data->otplata[$k]["id"] = $otpid;
				$this->_data->otplata[$k]["izvod"] = $_data["otplata_izvod"][$k];
				$this->_data->otplata[$k]["datum"] = array(
					"date" => $_data["otplata_date"][$k],
					"time" => date2int($_data["otplata_date"][$k])
				);
				$this->_data->otplata[$k]["bruto"]		= floatval(strip_number_format($_data["otplata_bruto"][$k]));
				$this->_data->otplata[$k]["neto"]		= floatval(strip_number_format($_data["otplata_neto"][$k]));
				$this->_data->otplata[$k]["porez"]		= floatval(strip_number_format($_data["otplata_porez"][$k]));
				$this->_data->otplata[$k]["kamata"]		= floatval(strip_number_format($_data["otplata_kamata"][$k]));
				$this->_data->otplata[$k]["troskovi"]	= floatval(strip_number_format($_data["otplata_troskovi"][$k]));
			}
		}
		
		//$this->debug(var_export($this->_data,true));
	}
	
	function novi(){
		
		$ima = $this->db->getOne("SELECT id FROM ugovori WHERE broj='".$this->_data->broj."' LIMIT 1");
		if ($ima>0) {
			return false;
		}
		
		$q = "INSERT INTO ugovori(broj) VALUES('".$this->_data->broj."')";
		if ($this->db->alterTable($q)) {
			$this->_data->id = $this->db->getOne("SELECT LAST_INSERT_ID() FROM ugovori");
			$this->setError(0,"");
			$this->log_change->state = 1;
			return true;
		}else {
			$this->setError(mysql_errno(), mysql_error());
			return false;
		}
	}
	
	function testData(){
		// broj ugovora
		if (!strlen($this->_data->broj)) {
			return "Morate uneti broj ugovora!";
		}
		// komitent
		if (!($this->_data->komitent["id"]>0)) {
			return "Morate odabrati komitenta!";
		}
		
		return true;
	}
	
	function generateQueries(){
		// UGOVORI
		$q = "UPDATE ugovori SET "
			."broj='".$this->_data->broj."',"
			."komitent=".$this->_data->komitent["id"].","
			."komitent_ziro='".$this->_data->komitent["ziro"]."',"
			."komitent_banka='".$this->_data->komitent["banka"]."',"
			."datum='".sprintf("%s",date("Y-m-d H:i:s",$this->_data->datum["time"]))."',"
			."datum_unosa='".date("Y-m-d H:i:s")."',"
			."cenovnik='".$this->_data->cenovnik["id"]."',"
			."lid='".$this->_data->lokacija["lid"]."',"
			."saradnik='".$this->_data->radnik."',"
			."porez='".$this->_data->porez["id"]."',"
			."tip_ugovora='".$this->_data->tip."',"
			."izrada_status='".$this->_data->state."',"
			."obnova_popust='".$this->_data->obnova_popust."',"
			."ne_obnova='".$this->_data->ne_obnova."',"
			."iznos_bruto='".$this->_data->cena["bruto"]."',"
			."iznos_neto='".$this->_data->cena["neto"]."',"
			."iznos_porez='".$this->_data->cena["porez"]."',"
			."iznos_popust='".$this->_data->cena["korekcija"]."',"
			."problem='".$this->_data->problem."',"
			."napomena='".addslashes($this->_data->napomena)."' "
			."WHERE id=".$this->_data->id;
		array_push($this->queries["update"],$q);
		
		// DOPISI
		array_push($this->queries["delete"],"DELETE FROM dopisi WHERE ugovor_id=".$this->_data->id);
		foreach ($this->_data->dopisi as $dopis){
			$q = "INSERT INTO dopisi VALUES('',"
				.$this->_data->id.","
				.$dopis["tip"].","
				.date2int($dopis["date"]).","
				.$dopis["radnik"].","
				.date2int($dopis["radnik_date"]).","
				.$dopis["ishod"].","
				.date2int($dopis["ishod_date"]).","
				."'".addslashes($dopis["napomena"])."'"
				.")";
			array_push($this->queries["insert"],$q);
		}
		
		// DEKLARACIJE
		array_push($this->queries["delete"],"DELETE FROM deklaracije WHERE ugovor=".$this->_data->id);
		foreach ($this->_data->deklaracije as $dekl){
			$q = "INSERT INTO deklaracije VALUES('',"
				.$this->_data->id.","
				."'".addslashes($dekl["creator"]["name"])."',"
				."'".addslashes($dekl["creator"]["address"])."',"
				."'".addslashes($dekl["creator"]["del_id"])."',"
				."'".addslashes($dekl["creator"]["del_name"])."',"
				."'".addslashes($dekl["creator"]["matbr"])."',"
				."'".addslashes($dekl["creator"]["odg_ime"])."',"
				."'".addslashes($dekl["creator"]["odg_address"])."',"
				."'".addslashes($dekl["creator"]["odg_jmbg"])."',"
				."'".addslashes($dekl["creator"]["odg_lk"])."',"
				."'".addslashes($dekl["owner"]["name"])."',"
				."'".addslashes($dekl["owner"]["address"])."',"
				."'".addslashes($dekl["owner"]["del_id"])."',"
				."'".addslashes($dekl["owner"]["del_name"])."',"
				."'".addslashes($dekl["owner"]["matbr"])."',"
				."'".addslashes($dekl["owner"]["odg_ime"])."',"
				."'".addslashes($dekl["owner"]["odg_address"])."',"
				."'".addslashes($dekl["owner"]["odg_jmbg"])."',"
				."'".addslashes($dekl["owner"]["odg_lk"])."',"
				.$dekl["message"]["type"].","
				."'".addslashes($dekl["message"]["phone"])."',"
				."'".addslashes($dekl["message"]["time"])."',"
				."'".addslashes($dekl["message"]["location"])."',"
				."'".addslashes($dekl["message"]["signature"])."',"
				."'".addslashes($dekl["message"]["text"])."'"
				.")";
			array_push($this->queries["insert"],$q);
		}
		
		// STAVKE
		
			// DELETE ALL STAVKE TABLES
			$sres = $this->db->select1("SELECT stavka_id FROM ugovori_stavke WHERE ugovor_id=".$this->_data->id);
			if ($sres !== false) {
				foreach ($sres as $key_stavka=>$srow){
					
					array_push($this->queries["delete"],"DELETE FROM ugovori_stavke WHERE stavka_id=".$srow[0]." LIMIT 1");
					
					$tres = $this->db->select1("SELECT tok_id FROM ugovori_tok_realizacije WHERE stavka=".$srow[0]);
					if ($tres !== false) {
						foreach ($tres as $key_tok=>$trow){
							array_push($this->queries["delete"],"DELETE FROM ugovori_tok_realizacije WHERE tok_id=".$trow[0]." LIMIT 1");
							array_push($this->queries["delete"],"DELETE FROM tok_trajanje_periodi WHERE id_tok=".$trow[0]);
							array_push($this->queries["delete"],"DELETE FROM tok_materijali WHERE tok=".$trow[0]);
							array_push($this->queries["delete"],"DELETE FROM ugovori_tok_actions WHERE tok=".$trow[0]);
							array_push($this->queries["delete"],"DELETE FROM tok_materijali WHERE tok=".$trow[0]);
							array_push($this->queries["delete"],"DELETE FROM specijalni_zahtevi WHERE id_tok=".$trow[0]);
							array_push($this->queries["delete"],"DELETE FROM tok_trajanje_periodi WHERE id_tok=".$trow[0]);
							array_push($this->queries["delete"],"DELETE FROM internet_data WHERE id_tok=".$trow[0]);
							array_push($this->queries["delete"],"DELETE FROM knjiga_data WHERE id_tok=".$trow[0]);
						}
					}
					//$this->debug("\n\n$key_stavka: ".var_export($tok_ids[$key_stavka]),FILE_APPEND);
				}
			}
		foreach ($this->_data->stavke as $key=>$stavka){
			// ugovori_stavke
			$q = "INSERT INTO ugovori_stavke VALUES('',"
				.$this->_data->id.","
				."'".$stavka["komitent"]["id"]."',"
				.$stavka["delatnost"].","
				.$stavka["kampanja"].","
				.$stavka["artikl"].","
				.$stavka["paket"].","
				.$stavka["kolicina"].","
				."'".($stavka["cena"]*$stavka["kolicina"])."',"
				."'".$stavka["popust"]."',"
				."'".$stavka["porez"]."',"
				."'".$stavka["bruto"]."'"
				.")";
			if ($this->db->alterTable($q)) {
				$stavkaid = $this->db->getOne("SELECT LAST_INSERT_ID() FROM ugovori_stavke");
			}else{
				die("1{!}".mysql_error()."\n\n".$q);
			}
			/*************************************
			 * 	PAKET
			 ************************************/
			if ($stavka["paket"]>0) {
				foreach ($stavka["tok_data"]["sifre_artikla"] as $k=>$sifra_artikla){
					
					// ugovori_tok_realizacije
					$q = "INSERT INTO ugovori_tok_realizacije VALUES('',"
						.$stavkaid.","
						."$sifra_artikla,"
						.(isset($stavka["tok_data"]["materijal"][$k])?1:0).","
						."'".$stavka["tok_data"]["materijal_ocekuje"][$k]["time"]."',"
						."'".$stavka["tok_data"]["komitent_poslato"][$k]["time"]."',"
						."'".$stavka["tok_data"]["komitent_potvrdjeno"][$k]["time"]."',"
						."'".$stavka["tok_data"]["komitent_obavesten"][$k]["time"]."',"
						."'".$stavka["tok_data"]["obnova"][$k]."',"
						."'".addslashes($stavka["tok_data"]["spec_zahtevi"][$k])."',"
						."'".addslashes($stavka["tok_data"]["napomena"][$k])."'"
						.")";
					if ($this->db->alterTable($q)) {
						$tokid = $this->db->getOne("SELECT LAST_INSERT_ID() FROM ugovori_tok_realizacije");
					}else{
						die("1{!}".mysql_error());
					}
					
					
					// tok_materijali
					if (count($stavka["tok_data"]["materijal"][$k])>0) {
						foreach ($stavka["tok_data"]["materijal"][$k] as $tok_mat){
							$q = "INSERT INTO tok_materijali VALUES('',"
								."$tokid,"
								."'".$tok_mat["tip_ulaza"]."',"
								."'".implode(";",$tok_mat["tip_materijala"])."',"
								."'".$tok_mat["upotrebljiv"]."',"
								."'".date2int($tok_mat["date"])."',"
								."'".addslashes($tok_mat["napomena"])."'"
								.")";
							array_push($this->queries["insert"],$q);
						}
					}
					// akcije dizajnera
					if (count($stavka["tok_data"]["akcije_dizajnera"][$k])>0) {
						foreach ($stavka["tok_data"]["akcije_dizajnera"][$k] as $desc_act){
							$q = "INSERT INTO ugovori_tok_actions VALUES('',"
								."$tokid,"
								."'".$desc_act["editor"]."',"
								."'".$desc_act["designer"]."',"
								."'".$desc_act["done"]."',"
								."'".addslashes($desc_act["designer_note"])."',"
								."'".date2int($desc_act["date_out"])."',"
								."'".date2int($desc_act["date_in"])."',"
								."'".addslashes($desc_act["napomena"])."'"
								.")";
							array_push($this->queries["insert"],$q);
						}
					}
					
					// internet data
						$inet = $stavka["tok_data"]["internet_data"];
						$q = "INSERT INTO internet_data VALUES('',"
							."$tokid,"
							."'".str_replace("'","\\'",$inet["kwords"][$k])."',"
							."'".$inet["profil"]["tip"][$k]."',"
							."'".$inet["profil"]["start"][$k]["time"]."',"
							."'".$inet["profil"]["end"][$k]["time"]."',"
							."'".addslashes($inet["profil"]["logo"][$k])."',"
							."'".addslashes($inet["profil"]["srp"][$k])."',"
							."'".addslashes($inet["profil"]["eng"][$k])."',"
							."'".$inet["baner"]["tip"][$k]."',"
							."'".$inet["baner"]["start"][$k]["time"]."',"
							."'".$inet["baner"]["end"][$k]["time"]."',"
							."'".addslashes($inet["baner"]["name"][$k])."',"
							."'".addslashes($inet["test_adresa"][$k])."',"
							."'".addslashes($inet["final_adresa"][$k])."',"
							."'".$inet["hosting"]["yp"]["start"][$k]["time"]."',"
							."'".$inet["hosting"]["yp"]["end"][$k]["time"]."',"
							."'".$inet["hosting"]["provajder"]["id"][$k]."',"
							."'".$inet["hosting"]["provajder"]["start"][$k]["time"]."',"
							."'".$inet["hosting"]["provajder"]["end"][$k]["time"]."',"
							."'".addslashes($inet["domen"]["ime"][$k])."',"
							."'".$inet["domen"]["start"][$k]["time"]."',"
							."'".$inet["domen"]["end"][$k]["time"]."'"
							.")";
						array_push($this->queries["insert"],$q);
						
						
						
						// periodi trajanja
						foreach ($inet["periodi"][$k] as $period){
							if($period["od"]["time"]>0 || $period["do"]["time"]>0){
								$q = "INSERT INTO tok_trajanje_periodi VALUES('',"
									."$tokid,"
									."'".$period["od"]["time"]."',"
									."'".$period["do"]["time"]."'"
									.")";
								array_push($this->queries["insert"],$q);
							}
						}
					// book_data
					$book = $stavka["tok_data"]["book_data"];
					$book_links = count($book["links"][$k])>0?";".implode(";",$book["links"][$k]).";":"";
					//$this->debug(var_export($book,true));
						$q = "INSERT INTO knjiga_data VALUES('',"
							."$tokid,"
							."'".$book["multi"]["on"][$k]."',"
							."'".intval($book["multi"]["num_pages"][$k])."',"
							."'".$book["multi"]["delatnost"][$k]."',"
							."'".addslashes($book["brands"][$k])."',"
							."'$book_links'"
							.")";
						array_push($this->queries["insert"],$q);
				}
			
			/*************************************
			 * 	NIJE PAKET
			 ************************************/
			}else{
				// ugovori_tok_realizacije
				$q = "INSERT INTO ugovori_tok_realizacije VALUES('',"
					.$stavkaid.","
					.$stavka["artikl"].","
					.(isset($stavka["tok_data"]["materijal"])?1:0).","
					."'".$stavka["tok_data"]["materijal_ocekuje"]["time"]."',"
					."'".$stavka["tok_data"]["komitent_poslato"]["time"]."',"
					."'".$stavka["tok_data"]["komitent_potvrdjeno"]["time"]."',"
					."'".$stavka["tok_data"]["komitent_obavesten"]["time"]."',"
					."'".$stavka["tok_data"]["obnova"]."',"
					."'".addslashes($stavka["tok_data"]["spec_zahtevi"])."',"
					."'".addslashes($stavka["tok_data"]["napomena"])."'"
					.")";
				if($this->db->alterTable($q)){
					$tokid = $this->db->getOne("SELECT LAST_INSERT_ID() FROM ugovori_tok_realizacije");
				}else{
					die("1{!}".mysql_error());
				}
				
				
				// tok_materijali
				if (count($stavka["tok_data"]["materijal"])>0) {
					foreach ($stavka["tok_data"]["materijal"] as $tok_mat){
						$q = "INSERT INTO tok_materijali VALUES('',"
							."$tokid,"
							."'".$tok_mat["tip_ulaza"]."',"
							."'".implode(";",$tok_mat["tip_materijala"])."',"
							."'".$tok_mat["upotrebljiv"]."',"
							."'".date2int($tok_mat["date"])."',"
							."'".addslashes($tok_mat["napomena"])."'"
							.")";
						array_push($this->queries["insert"],$q);
					}
				}
				
				// akcije dizajnera
				if (count($stavka["tok_data"]["akcije_dizajnera"])>0) {
					foreach ($stavka["tok_data"]["akcije_dizajnera"] as $desc_act){
						$q = "INSERT INTO ugovori_tok_actions VALUES('',"
							."$tokid,"
							.$desc_act["editor"].","
							.$desc_act["designer"].","
							.$desc_act["done"].","
							."'".addslashes($desc_act["designer_note"])."',"
							.date2int($desc_act["date_out"]).","
							.date2int($desc_act["date_in"]).","
							."'".addslashes($desc_act["napomena"])."'"
							.")";
						array_push($this->queries["insert"],$q);
					}
				}
				
				// internet_data
				$inet = $stavka["tok_data"]["internet_data"];
					$q = "INSERT INTO internet_data VALUES('',"
						."$tokid,"
						."'".str_replace("'","\\'",$inet["kwords"])."',"
						."'".$inet["profil"]["tip"]."',"
						."'".$inet["profil"]["start"]["time"]."',"
						."'".$inet["profil"]["end"]["time"]."',"
						."'".addslashes($inet["profil"]["logo"])."',"
						."'".addslashes($inet["profil"]["srp"])."',"
						."'".addslashes($inet["profil"]["eng"])."',"
						."'".$inet["baner"]["tip"]."',"
						."'".$inet["baner"]["start"]["time"]."',"
						."'".$inet["baner"]["end"]["time"]."',"
						."'".addslashes($inet["baner"]["name"])."',"
						."'".addslashes($inet["test_adresa"])."',"
						."'".addslashes($inet["final_adresa"])."',"
						."'".$inet["hosting"]["yp"]["start"]["time"]."',"
						."'".$inet["hosting"]["yp"]["end"]["time"]."',"
						."'".$inet["hosting"]["provajder"]["id"]."',"
						."'".$inet["hosting"]["provajder"]["start"]["time"]."',"
						."'".$inet["hosting"]["provajder"]["end"]["time"]."',"
						."'".addslashes($inet["domen"]["ime"])."',"
						."'".$inet["domen"]["start"]["time"]."',"
						."'".$inet["domen"]["end"]["time"]."'"
						.")";
					array_push($this->queries["insert"],$q);
				
					// periodi trajanja
					foreach ($inet["periodi"] as $period){
						if ($period["od"]["time"]>0 || $period["do"]["time"]>0) {
							$q = "INSERT INTO tok_trajanje_periodi VALUES('',"
								."$tokid,"
								."'".$period["od"]["time"]."',"
								."'".$period["do"]["time"]."'"
								.")";
							array_push($this->queries["insert"],$q);
						}
					}
				
				// book_data
				$book = $stavka["tok_data"]["book_data"];
				$book_links = count($book["links"])>0?";".implode(";",$book["links"]).";":"";
				$q = "INSERT INTO knjiga_data VALUES('',"
					."$tokid,"
					."'".$book["multi"]["on"]."',"
					."'".intval($book["multi"]["num_pages"])."',"
					."'".$book["multi"]["delatnost"]."',"
					."'".addslashes($book["brands"])."',"
					."'$book_links'"
					.")";
				array_push($this->queries["insert"],$q);
			}
		}
		
		// PLAN OTPLATE
		//$q = "DELETE ugovori_plan_otplate.*, ugovori_plan_rate.* FROM ugovori_plan_otplate,ugovori_plan_rate  WHERE ugovori_plan_otplate.plan_id=ugovori_plan_rate.rata_plan AND ugovori_plan_otplate.ugovor_id=".$this->_data->id;
		$plan_ids = $this->db->select2("SELECT plan_id FROM ugovori_plan_otplate WHERE ugovor_id=".$this->_data->id);
		if ($plan_ids !== false) {
			$q1 = "DELETE FROM ugovori_plan_otplate WHERE 0";
			foreach ($plan_ids as $v){
				$q1 .= " OR plan_id=".$v["plan_id"];
				
				$q = "DELETE FROM ugovori_plan_rate WHERE rata_plan=".$v["plan_id"];
				array_push($this->queries["delete"],$q);
			}
			array_push($this->queries["delete"],$q1);
		}
		
		$brrata = $this->_data->plan["nacin_placanja"]==2?0:$this->_data->plan["broj_rata"];
		
		$q = "INSERT INTO ugovori_plan_otplate VALUES('',"
			.$this->_data->id.","
			.intval($brrata).","
			.$this->_data->plan["nacin_placanja"].","
			."'".$this->_data->plan["avans"]["bruto"]."',"
			."'".$this->_data->plan["avans"]["neto"]."',"
			."'".$this->_data->plan["avans"]["porez"]."',"
			."'".int2date($this->_data->plan["avans_date"]["time"],"Y-m-d H:i:s")."'"
			.")";
		if ($this->db->alterTable($q)) {
			$planid = $this->db->getOne("SELECT LAST_INSERT_ID() FROM ugovori_plan_otplate");
		}else{
			die("1{!}".mysql_error());
		}
		
		if ($brrata>0) {
			foreach ($this->_data->plan["rate"] as $rata){
				$q = "INSERT INTO ugovori_plan_rate VALUES('',"
					."$planid,"
					."'".$rata["neto"]."',"
					."'".$rata["porez"]."',"
					."'".$rata["bruto"]."',"
					."'".$rata["kamata"]."',"
					."'".$rata["troskovi"]."',"
					."'".$rata["datum"]["time"]."',"
					."'".$rata["potvrda"]."'"
					.")";
				array_push($this->queries["insert"],$q);
			}
		}
		
		// OTPLATA
		$q = "DELETE FROM ugovori_otplate WHERE ugoid=".$this->_data->id;
		array_push($this->queries["delete"],$q);
		if (is_array($this->_data->otplata)) {
			//$this->debug(var_export($this->_data->otplata,true));
			//file_put_contents("tmpvoja",var_export($this->_data->otplata,true));
			foreach ($this->_data->otplata as $otplata){
				$q = "INSERT INTO ugovori_otplate VALUES("
					.$this->_data->id.","
					.$otplata["datum"]["time"].","
					."'".$otplata["bruto"]."',"
					."'".$otplata["neto"]."',"
					."'".$otplata["porez"]."',"
					."'".$otplata["izvod"]."',"
					."'".$otplata["kamata"]."'"
					.")";
				array_push($this->queries["insert"],$q);
			}
		}
	}
	
	function write(){
		//$this->debug(implode("\n",$this->queries)); return true;
		foreach ($this->queries as $k=>$v){
			if (count($v)) {
				foreach ($v as $q){
					if (!$this->db->alterTable($q)) {
						$this->setError(mysql_errno(),mysql_error()."\n\n$k: $q");
						return false;
					}
				}
			}
		}
		$this->log_change->user = $this->getUserId();
		$this->log_change->ip = $_SERVER['REMOTE_ADDR'];
		$this->log_change->time = time();
		$this->log_change->table = "ugovori";
		$this->log_change->attr = "id";
		$this->log_change->value = $this->_data->id;
		$this->log_change->write();
		
		return true;
	}

	//----------------------------------------------------------
	
	function setError($code,$msg=""){
		$this->error = array(
			"code" => intval($code),
			"message" => $msg
		);
	}
	
	function debug($msg,$opts=null){
		file_put_contents("tmpVOJA",$msg,$opts);
	}
}

?>